package cn.goktech.util;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 封装统一返回结果
 * 一般包含：返回状态码 ：200-成功  500-失败
 *          返回信息 ：成功或失败
 *          结果集：K-V键值对
 *          成功标识：success-成功或失败
 */
@Data
public class ResultUtil {
    private boolean success;//请求状态
    private Integer code;//状态码
    private String msg;//返回信息
    private Map<String,Object> data;

    //构造器私有化-单例模式思想-用户不能随意创建对象
    private ResultUtil() {
    }

    public static ResultUtil ok(){
        ResultUtil rs = new ResultUtil();
        rs.setCode(IResultCode.SUCCESS);
        rs.setSuccess(true);
        rs.setMsg("请求成功");
        rs.data = new HashMap<>();
        return rs;
    }

    public static ResultUtil error(){
        ResultUtil rs = new ResultUtil();
        rs.setCode(IResultCode.ERROR);
        rs.setSuccess(false);
        rs.setMsg("请求失败");
        rs.data = new HashMap<>();
        return rs;
    }

    //设置值返回this-支持链式调用
    public ResultUtil success(Boolean success){
        this.setSuccess(success);
        return this;
    }
    public ResultUtil message(String msg){
        this.setMsg(msg);
        return this;
    }
    public ResultUtil code(Integer code){
        this.setCode(code);
        return this;
    }
    public ResultUtil data(String key, Object value){
        this.data.put(key, value);
        return this;
    }
    public ResultUtil data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}
